<?php

namespace dao;

use dao\fcfDao;
use utils\Utils;
use controladores\Logger;



class galeriasDao extends fcfDao{

	public function subir_fotos($nom, $type, $size, $id_galeria){
	   $queryString = "INSERT INTO fotos (id_galeria, nom, descripcio, type, size) VALUES ('$id_galeria', '$nom', '', '$type', '$size')";
	   return $this->insert($queryString);
       }
    
    public function crear_galeria($nom, $seccion){
        $data = date("Y-m-d H:i:s"); 
	   $queryString = "INSERT INTO galerias (nom, visible, data, seccion) VALUES ('$nom', 1, '$data', '$seccion')";
	   $this->insert($queryString);
        $queryString = "SELECT MAX(id) AS id FROM galerias";
        return $this->select($queryString);
       }
    
    public function crear_seccion($nom){
	   $queryString = "INSERT INTO galerias_secciones (nom) VALUES ('$nom')";
	   return $this->insert($queryString);
       }
    
    public function galeria_exist($nom){
       $queryString = "SELECT * FROM galerias WHERE nom = '$nom'";
	   return count($this->select($queryString)); 
    } 
    
    public function activar_galeria($id, $estado){
       $queryString = "UPDATE galerias SET visible = '$estado' WHERE id = '$id'";
	   return $this->update($queryString); 
    }    
       
    public function get_galeria($id){
	   $queryString = "SELECT * FROM galerias WHERE id = '$id'";
	   return $this->select($queryString);
       }
       
    public function get_seccions(){
	   $queryString = "SELECT * FROM galerias_secciones";
	   return $this->select($queryString);
       }
    
    public function get_seccio($id){
	   $queryString = "SELECT * FROM galerias_secciones WHERE id = '$id'";
	   return $this->select($queryString);
       }
       
    public function get_fotos($id){
	   $queryString = "SELECT *, (SELECT nom FROM galerias WHERE id = '$id') AS nom_galeria FROM fotos WHERE id_galeria = '$id'";
	   return $this->select($queryString);
       }
    
    public function get_foto($id){
	   $queryString = "SELECT * FROM fotos WHERE id = '$id'";
	   return $this->select($queryString);
       }
       
    public function get_galeries(){
	   $queryString = "SELECT a.id AS id, a.nom AS galeria, a.data AS fecha, b.nom AS seccion, 
                        (SELECT nom FROM `fotos` WHERE id_galeria = a.id LIMIT 1) AS foto1, 
                        (SELECT nom FROM `fotos` WHERE id_galeria = a.id AND nom != foto1 LIMIT 1) AS foto2,
                        (SELECT nom FROM `fotos` WHERE id_galeria = a.id AND nom != foto1 AND nom != foto2 LIMIT 1) AS foto3,
                        (SELECT nom FROM `fotos` WHERE id_galeria = a.id AND nom != foto1 AND nom != foto2 AND nom != foto3 LIMIT 1) AS foto4
                        FROM galerias AS a LEFT JOIN galerias_secciones AS b 
                        ON a.seccion = b.id 
                        ";
	   return $this->select($queryString);
       }
    
    public function eliminar_galeria($id){
	   $queryString = "DELETE FROM galerias_secciones WHERE id = '$id'";
	   return $this->delete($queryString);
       }
}



?>